package com.flying.pas.security.service;

import java.util.Date;
import java.util.List;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.pas.security.model.RolePerm;
import com.flying.pas.security.model.RoleUser;
import com.flying.pas.security.model.User;

@Service(value="RolePermService", desc="")
public class RolePermService extends AbstractService{
	private RoleUserService rus;
	
	public void setRoleUserService(RoleUserService s) {
		this.rus = s;
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=RolePerm.ENTITY_NAME)
	public RolePerm create(
			@Param(value=RolePerm.PERM_ID, required=true,	desc="权限ID") Long perm_id,
			@Param(value=RolePerm.ROLE_ID, required=true,	desc="角色ID") Long role_id,
			@Param(value=RolePerm.CREATE_TIME, required=false,	desc="CREATE TIME") Date create_time
		) throws Exception {
		return ModelFactory.create(RolePerm.class, 
				RolePerm.CREATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=RolePerm.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=RolePerm.PERM_ID, required=true,	desc="权限ID") Long perm_id,
			@Param(value=RolePerm.ROLE_ID, required=true,	desc="角色ID") Long role_id
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=RolePerm.ENTITY_NAME)
	public ActionResult removeByRole(
			@Param(value=RolePerm.ROLE_ID, required=true,	desc="角色ID") Long role_id
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=RolePerm.ENTITY_NAME)
	public ActionResult removeByPerm(
			@Param(value=RolePerm.PERM_ID, required=true,	desc="权限ID") Long perm_id
		) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=RolePerm.ENTITY_NAME, pageable=true, modelClass=RolePerm.class)
	public QueryResult<RolePerm> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=RolePerm.ENTITY_NAME, pageable=false, modelClass=RolePerm.class)
	public QueryResult<RolePerm> findByRole(
			@Param(value=RolePerm.ROLE_ID, required=true,	desc="角色ID") Long role_id) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=RolePerm.ENTITY_NAME, pageable=false, modelClass=RolePerm.class)
	public QueryResult<RolePerm> findByRoleIds(
			@Param(value=RolePerm.ROLE_ID, required=true,	desc="角色IDs", tag="IN") Long[] role_ids) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	public QueryResult<RolePerm> findByUserId(
			@Param(value=User.USER_ID, 	required=true, desc="用户ID") Long user_id) throws Exception {
		List<RoleUser> roles = this.rus.findByUserId(user_id).getRows();
		Long[] roleIds = roles.stream().map(x -> x.getRoleId()).toArray(Long[]::new);
		return this.findByRoleIds(roleIds);
	}

	@MethodInfo("查询")
	@DaoQuery(entity=RolePerm.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public RolePerm findById(
			@Param(value=RolePerm.PERM_ID, required=true,	desc="权限ID") Long perm_id,
			@Param(value=RolePerm.ROLE_ID, required=true,	desc="角色ID") Long role_id
		) throws Exception {
		return null;
	}

}